package org.jdu.dao.metadata;

import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

public class DBMetaData implements Serializable {

	private static final long serialVersionUID = -2598968516288006814L;

	public static final String TABLE_CAT = "TABLE_CAT";
	public static final String TABLE_SCHEM = "TABLE_SCHEM";
	public static final String TABLE_NAME = "TABLE_NAME";
	public static final String COLUMN_NAME = "COLUMN_NAME";
	public static final String DATA_TYPE = "DATA_TYPE";
	public static final String TYPE_NAME = "TYPE_NAME";
	public static final String COLUMN_SIZE = "COLUMN_SIZE";
	public static final String DECIMAL_DIGITS = "DECIMAL_DIGITS";
	public static final String NUM_PREC_RADIX = "NUM_PREC_RADIX";
	public static final String NULLABLE = "NULLABLE";

	public static final String REMARKS = "REMARKS";
	public static final String COLUMN_DEF = "COLUMN_DEF";
	public static final String SQL_DATA_TYPE = "SQL_DATA_TYPE";
	public static final String SQL_DATETIME_SUB = "SQL_DATETIME_SUB";
	public static final String CHAR_OCTET_LENGTH = "CHAR_OCTET_LENGTH";
	public static final String ORDINAL_POSITION = "ORDINAL_POSITION";
	public static final String IS_NULLABLE = "IS_NULLABLE";
	public static final String SCOPE_CATLOG = "SCOPE_CATLOG";
	public static final String SCOPE_SCHEMA = "SCOPE_SCHEMA";
	/**
	 * 
	 */

	public static final String TABLE_TYPE = "TABLE_TYPE";
	public static final String TYPE_CAT = "TYPE_CAT";
	public static final String TYPE_SCHEM = "TYPE_SCHEM";
	public static final String SELF_REFERENCING_COL_NAME = "SELF_REFERENCING_COL_NAME";
	public static final String REF_GENERATION = "REF_GENERATION";

	private TableMetaData tableMetaData;


	public TableMetaData getTableMetaData(ResultSet rs) throws SQLException {
		ResultSetMetaData rsmd = rs.getMetaData();
		tableMetaData = new TableMetaData();
		int cols = rsmd.getColumnCount();
		Collection columns = new ArrayList();
		for (int c = 0; c < cols; c++) {
			columns.add(rsmd.getColumnName(c + 1));
		}
		if (rs.next()) {
			if (columns.contains(DBMetaData.TABLE_CAT)) {
				tableMetaData.setTableCatalog( rs.getString(DBMetaData.TABLE_CAT));
			}
			if (columns.contains(DBMetaData.TABLE_SCHEM)) {
				tableMetaData.setTableSchema(rs.getString(DBMetaData.TABLE_SCHEM));
			}
			if (columns.contains(DBMetaData.TABLE_NAME)) {
				tableMetaData.setTableName(rs.getString(DBMetaData.TABLE_NAME));
			}
			if (columns.contains(DBMetaData.TABLE_TYPE)) {
				tableMetaData.setTableType( rs.getString(DBMetaData.TABLE_TYPE));
			}
			if (columns.contains(DBMetaData.REMARKS)) {
				tableMetaData.setRemarks(rs.getString(DBMetaData.REMARKS));
			}
			if (columns.contains(DBMetaData.TYPE_CAT)) {
				tableMetaData.setTypeCat(rs.getString(DBMetaData.TYPE_CAT));
			}
			if (columns.contains(DBMetaData.TYPE_SCHEM)) {
				tableMetaData.setTypeSchema(rs.getString(DBMetaData.TYPE_SCHEM));
			}
			if (columns.contains(DBMetaData.TYPE_NAME)) {
				tableMetaData.setTypeName(rs.getString(DBMetaData.TYPE_NAME));
			}
			if (columns.contains(DBMetaData.SELF_REFERENCING_COL_NAME)) {
				tableMetaData.setSelfReferencingColName(rs
						.getString(DBMetaData.SELF_REFERENCING_COL_NAME));
			}
			if (columns.contains(DBMetaData.REF_GENERATION)) {
				tableMetaData.setRefGeneration(rs.getString(DBMetaData.REF_GENERATION));
			}
		}
		return tableMetaData;
	}

}
